<?php

class Core_Table_Tag_Keyword extends Kd_Data_Table {

    public function __construct() {
        parent::__construct();
    }

    public function statRecord($keyword, $accuracy = FALSE){
        $sql = 'select count(record_id) as `total`, record_type as `type` from tag_searchable where tag_id in(
select id from tag_keyword where `name` like :keyword:s)group by record_type';
        $arg = $accuracy? $keyword: '%'.$keyword.'%';
        return $this->getDbDriver()
                        ->setSql($sql, array('keyword'=>$arg))
                        ->fetch();
    }

    public function searchRecordIds($keyword, $type, $accuracy = FALSE){
        $query = Kd_Data_Factory::getQueryDcl()->from('{t}tag_searchable');
        $subQuery = $query->where()->inQuery('tag_id')
                        ->from('{t}tag_keyword')
                        ->field('id');
        if($accuracy){
            $subQuery->eq('name', ':keyword:s')->addParameter('keyword', $keyword);
        }else{
            $subQuery->like('name', ':keyword:s')->addParameter('keyword', '%'.$keyword.'%');
        }
        $subQuery->eq('record_type', ':type:s')->addParameter('type', $type);
        return $query->fetch();
    }

    public function insertKeyword($word){
        $count = Kd_Data_Factory::getQueryDcl()
                    ->from($this->getName())
                    ->field(Kd_Data_Dcl_Fn::count())
                    ->eq('name', ':name:s')
                    ->addParameter('name', $word)
                    ->fetch()
                    ->getFirstValue();
        if($count == 0){
            Kd_Data_Factory::getInsertDcl()
                ->into($this->getName())
                ->value('name', ':name:s')
                ->value('search_count', 1)
                ->addParameter('name', $word)
                ->execute();
        }else{
            Kd_Data_Factory::getUpdateDcl()
                ->into($this->getName())
                ->value('search_count', Kd_Data_Dcl_Fn::addition('search_count', 1))
                ->eq('name', ':name:s')
                ->addParameter('name', $word)
                ->execute();
        }
        return $this;
    }

    public function insertTagRecord(array $tagIds, $recordId, $recordType, $transaction = NULL) {
        $sql = Kd_Data_Factory::getInsertDcl()
                ->into('{t}tag_searchable')
                ->value('tag_id', ':tagId:i')
                ->value('record_id', ':recordId:s')
                ->value('record_type', ':recordType:s')
                ->toSql();
        $driver = $this->getDbDriver($transaction);
        foreach($tagIds as $id){
            $driver->setSql($sql, array('tagId' => $id, 'recordId' => $recordId, 'recordType' => $recordType))->execute();
        }
    }

    public function deleteTagRecord($recordId, $recordType, $transaction = NULL) {
        $dcl = Kd_Data_Factory::getDeleteDcl()
                ->from('{t}tag_searchable')
                ->where()
                    ->eq('record_id', ':recordId:s')
                    ->eq('record_type', ':recordType:s')
                ->closest()
                ->addParameter(array('recordId' => $recordId, 'recordType' => $recordType))
                ->setDbDriver($this->getDbDriver($transaction))
                ->execute();
    }

}
